#!/bin/tcsh

# DCtest1 (29 Aug 2006)
# This program runs an algorithm for all files in a corpus 
# It's output is processed by DCtest2

# the current version assumes that if the compressor is called
# XYZ the decompressor is unXYZ
# see decompression loop below

# todo: change this script to bash

#echo $* # for testing!  do not use with DCtest2

if( $#argv < 3 ) then
    echo "Usage: DCtest1 algorithm corpus num_runs [options]"
    exit 1;
endif

if ($#argv>3) then
  set options = "$argv[4-]"
else
  set options = ""
endif


set program = $argv[1]
set corpus = $argv[2]
set num_runs = $argv[3]

# build list of file names
set files=""
foreach filename (`cat $corpus/.index`)
  set files="$files $corpus/$filename"
end

set  end_of_file='--xx--'
set  end_of_benchmark='--==--'

echo Experiment run by `whoami` at `date` on `hostname`
echo Experiments with algorithm $program $options on corpus $corpus 

set num = `echo $files |wc -w`
echo $num files in corpus
echo $num_runs time measurements


###### this is where we run the algorithm 
foreach file ($files)
   set i=$num_runs
   # ----- compression
   while ($i != 0)
	/usr/bin/time -f"Elapsed time: %U user %S system (secs)"\
        $program $options $file -o $file.test > /dev/null
	if($status) then
	  echo Error working on file $file
          exit 2
	endif
	@ i--
   end #while
   set i=$num_runs
   # ----- decompression
   while ($i != 0)
	/usr/bin/time -f"Elapsed time: %U user %S system (secs)"\
          un$program $file.test -o tmpfile > /dev/null
	if($status) then
	  echo Error working on file $file
          exit 2
	endif
	@ i--
   end #while
   # ---- check compression/decompression
  #diff -i $file tmpfile > /dev/null
   cmp $file tmpfile > /dev/null
   if($status) then
      echo Decompression failed
   else
      echo Decompression OK
   endif
   # scrivi dimensioni
   echo Original size: `wc -c $file`
   echo Compressed size: `wc -c $file.test`
   echo $end_of_file 
   rm -f $file.test
   rm -f tmpfile
end #foreach file ($corpus)
echo $end_of_benchmark









